package com.yunkaweilai.android.view.dialog.shop;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.github.lazylibrary.util.ToastUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.operation.integral.IntegralTypeActivity;
import com.yunkaweilai.android.activity.shop.ShopTypeActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;

import org.greenrobot.eventbus.EventBus;


/**
 * 添加大分类
 * Created by Administrator on 2016/12/22 0022.
 */

public class ShopTypeMaxDialog extends Dialog {
    ImageView idImgClose;
    TextView idTvRight;
    EditText idEdtTypeName;
    private Context context;
    private String type;

    public ShopTypeMaxDialog(Context context, String type) {
        super(context,R.style.dialog);
        this.context = context;
        this.type = type;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_type_max_add);
        idImgClose = (ImageView)findViewById(R.id.id_img_close);
        idTvRight = (TextView) findViewById(R.id.id_tv_right);
        idEdtTypeName = (EditText) findViewById(R.id.id_edt_type_name);

        initview();
    }


    public void initview() {
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(idEdtTypeName.getText().toString())) {
                    if (type.equals(ShopTypeActivity.PT_SHOP)) {
                        addType(Api.PT_SHOP_TYPE_SAVE);
                    } else if (type.equals(ShopTypeActivity.TIME_SHOP)) {
                        addType(Api.TIME_SHOP_TYPE_SAVE);
                    } else if (type.equals(IntegralTypeActivity.INTEGRAL)) {
                        addType2(Api.APP_SAVE_INTEGRAL_CATEGORY);
                    }
                }
            }
        });
    }

    private void addType(String url) {
        HttpRequest.postUrl(url)
                .addParams("goods_category_name", idEdtTypeName.getText().toString())
                .addParams("status", "1")
                .addParams("sort","0")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode) {
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.MAX_TYPE, true));
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.SHOP_LIST_REFRESH, true));
                        }
                        dismiss();
                    }

                    @Override
                    public void onError(Exception e) {
                        ToastUtils.showToast(context, "添加分类失败");
                    }
                });

    }

    private void addType2(String url) {
        HttpRequest.postUrl(url)
                .addParams("gift_category_name", idEdtTypeName.getText().toString())
                .addParams("status", "1")
                .addParams("sort","0")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode) {
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.MAX_TYPE, true));
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.SHOP_LIST_REFRESH, true));
                        }
                        dismiss();
                    }

                    @Override
                    public void onError(Exception e) {
                        ToastUtils.showToast(context, "添加分类失败");
                    }
                });

    }

}
